# -*- coding: utf-8 -*-            
# @Time : 2024/6/10 16:25
# @Author  : lining
# @FileName: pageOpt.py
import time
from Handle.handle_log import log
import os
from Handle.handle_yaml import SCREENSHOT_PATH


class Page:
    def __init__(self, page):
        self.page = page

    # 获取标题
    def page_get_title(self):
        try:
            title = self.page.title()
            log.log_info(f'获取title成功:{title}')
            return title
        except Exception as e:
            log.log_error(f'获取title失败:{e}')
            raise e

    # 跳转页面
    def page_goto(self, url, timeout=None, wait_until=None, referer=None):
        try:
            self.page.goto(url=url, timeout=timeout, wait_until=wait_until, referer=referer)
            log.log_info(f'跳转{url}成功')
        except Exception as e:
            log.log_error(f'跳转{str(url)}失败:{e}')
            raise e

    # 执行js
    def page_execute_script(self, script, args=None):
        """
        :param script: 要执行的js
        :param args: 传给js的参数,列表格式
        :return:
        """
        try:
            value = self.page.evaluate(script, args)
            log.log_info(f'执行js {script}成功')
            return value
        except Exception as e:
            log.log_error(f'执行js {script}失败：'+repr(e))
            raise e

    # 关闭页面
    def page_close(self):
        try:
            self.page.close()
            log.log_info('关闭页面成功')
        except Exception as e:
            log.log_error('关闭页面失败'+repr(e))
            raise e

    # frame定位
    def page_frame(self, name=None, url=None):
        """
        :param name:
        :param url:
        :return: 返回匹配的frame，可直接click()等操作
        """
        try:
            frame = self.page.frame(name=name, url=url)
            log.log_info('frame定位成功')
            return frame
        except Exception as e:
            log.log_error('frame定位失败：'+repr(e))
            raise e

    def page_frames_locator(self, selector):
        """
        定位到frame上
        :param selector:
        :return: 返回第一个一个locator,需要再接定位器才能操作
        """
        try:
            frame = self.page.frame_locator(selector).first
            log.log_info('frame定位成功')
            return frame
        except Exception as e:
            log.log_error('frame定位失败：'+repr(e))
            raise e

    # 跳回主frame
    def page_main_frame(self):
        try:
            self.page.main_frame()
            log.log_info('跳回主frame成功')
        except Exception as e:
            log.log_error('跳回主frame失败:'+repr(e))
            raise e

    # 获取所有frame
    def page_frames(self):
        try:
            frames = self.page.frames
            log.log_info('获取所有frame成功')
            return frames
        except Exception as e:
            log.log_error('获取所有frame失败:'+repr(e))
            raise e

    # 页面前进
    def page_go_forward(self):
        try:
            self.page.go_forward()
            log.log_info('前进成功')
        except Exception as e:
            log.log_error('前进失败'+repr(e))
            raise e

    # 页面后退
    def page_go_back(self):
        try:
            self.page.go_back()
            log.log_info('后退成功')
        except Exception as e:
            log.log_error('后退失败'+repr(e))
            raise e

    # 页面重载
    def page_reload(self):
        try:
            self.page.reload()
            log.log_info('页面重载成功')
        except Exception as e:
            log.log_error('页面重载失败'+repr(e))
            raise e

    # 将页面置于最前面
    def page_bring_to_front(self):
        try:
            self.page.bring_to_front()
            log.log_info('将页面置于最前面成功')
        except Exception as e:
            log.log_error('将页面置于最前面失败'+repr(e))
            raise e

    # 页面截图
    def page_screenshot(self, name=None, ele=None, path=SCREENSHOT_PATH, img_type='png'):
        """
        :param ele: 截取指定元素的图片
        :param name: 图片名称，不填默认用时间
        :param path: 图片保存地址
        :param img_type: 图片格式，默认png
        :return:
        """
        if name is None:
            name = str(time.strftime('%Y%m%d%H%M%S'))
        path = os.path.join(path, name, '.', img_type)
        try:
            if ele:
                ele.screenshot(type=img_type, path=path)
            else:
                self.page.screenshot(type=img_type, path=path, full_page=True)
            log.log_info('页面截图成功')
        except Exception as e:
            log.log_error('页面截图失败：'+repr(e))
            raise e

    # 鼠标事件
    # 鼠标点击
    def page_mouse_click(self, x, y):
        """

        :param x: 横坐标
        :param y: 纵坐标
        :return:
        """
        try:
            self.page.mouse.click(x, y)
            log.log_info('鼠标点击成功')
        except Exception as e:
            log.log_error('鼠标点击成功'+repr(e))
            raise e

    # 鼠标移动到x,y位置
    def page_mouse_move(self, x, y, step=10):
        """
        :param x: 横坐标
        :param y: 纵坐标
        :param step: 移动步数
        :return:
        """
        try:
            self.page.mouse.move(x=x, y=y, steps=step)
            log.log_info(f'鼠标移动到位置{str([x, y])}成功')
        except Exception as e:
            log.log_error(f'鼠标移动到位置{str([x, y])}失败'+repr(e))
            raise e

    # 释放鼠标点击
    def page_mouse_up(self, button='left'):
        """
        默认释放鼠标左键
        :return:
        """
        try:
            self.page.mouse.up(button=button)
            log.log_info('释放鼠标点击成功')
        except Exception as e:
            log.log_error('释放鼠标点击失败'+repr(e))
            raise e

    # 鼠标按下
    def page_mouse_down(self, button='left'):
        try:
            self.page.mouse.down(button=button)
            log.log_info('鼠标按下成功')
        except Exception as e:
            log.log_error('鼠标按下失败'+repr(e))
            raise e
